package com.yoomiito.app.widget.wheelview;

import android.app.Dialog;
import android.content.Context;

import androidx.annotation.NonNull;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yoomiito.app.R;

import java.util.List;


/**
 * Created by Administrator on 2017/11/21 0021 19:03.
 */

public class BottomAreaSelectDialog extends Dialog implements OnWheelChangedListener {

    private int mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
    private int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;

    private int yearIndex;
    private int monthIndex;
    private int dayIndex;
    private Context mContext;

    private String[] years, months, days;
    private WheelView wvYear, wvMonth, wvDay;
    private String defaultSelectYear="河南省",
            defaultSelectMonth="郑州市",
            defaultSelectDay="中原区";
    private TextView cancelTv, sureTv;
    private List<Area.Province> provinceList;
    private List<Area.City> cityList;
    private Area area;

    public BottomAreaSelectDialog(@NonNull Context context, Area area) {
        this(context, 0, area);
    }

    public BottomAreaSelectDialog(@NonNull Context context, int theme, Area area) {
        super(context, R.style.BottomDialogStyle);
        this.mContext = context;
        this.area = area;
        init();
    }

    private void init() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
//        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
//        WindowManager wm = window.getWindowManager();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp= window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_area, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mWidth, mHeight);
        setContentView(view, lp);

//        cancelTv = view.findViewById(R.id.item_area_cancel);
//        sureTv = view.findViewById(R.id.item_area_sure);
//        cancelTv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                BottomAreaSelectDialog.this.dismiss();
//            }
//        });
//        sureTv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                BottomAreaSelectDialog.this.dismiss();
//                listenr.onResult(defaultSelectYear + defaultSelectMonth + defaultSelectDay);
//            }
//        });
//
//        wvYear = view.findViewById(R.id.item_area_wv_1);
//        wvMonth = view.findViewById(R.id.item_area_wv_2);
//        wvDay = view.findViewById(R.id.item_area_wv_3);

        initData();

        setWheelView(wvYear, years, yearIndex);
        setWheelView(wvMonth, months, monthIndex);
        setWheelView(wvDay, days, dayIndex);

    }

    private void setWheelView(WheelView wheelView, String[] entries, int currentIndex) {
        wheelView.setEntries(entries);
        wheelView.setVisibleItems(6);
        wheelView.setCyclic(false);
        wheelView.setCurrentIndex(currentIndex);
        wheelView.setOnWheelChangedListener(this);
    }

    @Override
    public void onChanged(WheelView view, int oldIndex, int newIndex) {
        switch (view.getId()) {
//            case R.id.item_area_wv_1:
//                defaultSelectYear = view.getItem(newIndex).toString();
//                updateCity(newIndex);
//                wvMonth.setCurrentIndex(0);
//                updateDistracts(0);
//                break;
//            case R.id.item_area_wv_2:
//                defaultSelectMonth = view.getItem(newIndex).toString();
//                updateDistracts(newIndex);
//                break;
//            case R.id.item_area_wv_3:
//                defaultSelectDay = view.getItem(newIndex).toString();
//                break;
        }

    }


    public void show() {
        initView();
        super.show();
    }

    private void initData() {
        years = area.getProvince();
        provinceList = area.getProvinces();

        int[] positions = Utils.getPositionOfArea(area, defaultSelectYear, defaultSelectMonth, defaultSelectDay);
        yearIndex = positions[0];
        monthIndex = positions[1];
        dayIndex = positions[2];
        updateCity(yearIndex);
        updateDistracts(monthIndex);
    }

    private void updateCity(int position) {
        Area.Province province = provinceList.get(position);
        months = province.getCitys();
        cityList = province.getCities();
        wvMonth.setEntries(months);
    }


    private void updateDistracts(int position) {
        Area.City city = cityList.get(position);
        days = city.getDistracts();
        wvDay.setEntries(days);
    }


    /**
     * 设置dialog的宽
     *
     * @param mWidth
     * @return
     */
    public BottomAreaSelectDialog setmWidth(int mWidth) {
        this.mWidth = Utils.dip2px(mContext, mWidth);
        return this;
    }

    /**
     * 设置dialog的高
     *
     * @param mHeight
     * @return
     */
    public BottomAreaSelectDialog setmHeight(int mHeight) {
        this.mHeight = Utils.dip2px(mContext, mHeight);
        return this;
    }

    public BottomAreaSelectDialog setDefaultSelectDate(String province, String city, String distract) {
        this.defaultSelectYear = province;
        this.defaultSelectMonth = city;
        this.defaultSelectDay = distract;
        return this;
    }

    OnAreaWheelViewListener listenr;

    public BottomAreaSelectDialog setListenr(OnAreaWheelViewListener listenr) {
        this.listenr = listenr;
        return this;
    }

    public interface OnAreaWheelViewListener {
        void onResult(String result);
    }
}
